package com.ship.boats.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ship.boats.domain.SpBoatsPortMessage;
import com.ship.boats.domain.SpPortInformation;
import com.ship.boats.domain.dto.QueryPortMessageRq;
import com.ship.boats.domain.dto.QueryTimeZoneSwitchingRq;
import com.ship.boats.domain.dto.SavePortInfoDto;
import com.ship.boats.domain.dto.UpdatePortMessageRq;
import com.ship.boats.domain.vo.QueryPortMessageInfo;
import com.ship.boats.domain.vo.QueryPortNameInfo;
import com.ship.boats.domain.vo.QueryTimeZoneSwitchingInfo;
import com.ship.boats.mapper.BoatsPortMessageMapper;
import com.ship.boats.service.BoatsPortMessageService;
import com.ship.boats.service.PortInformationService;
import com.ship.common.core.utils.StringUtils;
import com.ship.common.security.utils.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.*;
import java.util.stream.Collectors;
import java.time.LocalDateTime;

/**
 * <b>BoatsPortMessageServiceImpl</b>
 *
 * @description: BoatsPortMessageServiceImpl <br>
 * @date: 2024/4/29 14:30 <br>
 * @author: lhm <br>
 * @version: 1.0 <br>
 */

@Service
public class BoatsPortMessageServiceImpl extends ServiceImpl<BoatsPortMessageMapper, SpBoatsPortMessage> implements BoatsPortMessageService {
    private static final Logger logger = LoggerFactory.getLogger(BoatsPortMessageServiceImpl.class);

    @Autowired
    private PortInformationService portInformationService;

    @Override
    @Transactional
    public boolean createPortInformation(List<SpBoatsPortMessage> portList) {
        if (CollectionUtil.isNotEmpty(portList)){
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("enable","1");
            baseMapper.delete(queryWrapper);
            List<SavePortInfoDto> portInfoDtos = new ArrayList<>();
            portList.forEach(item -> {
                item.setEnable("1");
                item.setCreateUser(SecurityUtils.getUsername());
                item.setCreateTime(new Date());
                item.setUpdateUser(SecurityUtils.getUsername());
                item.setUpdateTime(new Date());
                SavePortInfoDto savePortInfoDto = new SavePortInfoDto();
                savePortInfoDto.setPortNameChinese(item.getPortNameCn());
                savePortInfoDto.setPortNameEnglish(item.getPortNameEn());
                savePortInfoDto.setPortCountry(item.getCountryEn());
                savePortInfoDto.setBelongPortNameEnglish(item.getPortNameEn());
                savePortInfoDto.setLon(item.getLon());
                savePortInfoDto.setLat(item.getLat());
                portInfoDtos.add(savePortInfoDto);
            });
            portInformationService.savePortInfoList(portInfoDtos,1);
            boolean b = this.saveBatch(portList);
            return b;
        }
        return false;
    }

    @Override
    public List<String> queryPortMessageConditions(String type) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("enable","1");
        List<SpBoatsPortMessage> portMessageList = baseMapper.selectList(queryWrapper);
        List<String> returnList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(portMessageList)){
            if (type.equals("portName")) {
                List<String> list = portMessageList.stream().map(SpBoatsPortMessage::getPortNameEn).distinct().collect(Collectors.toList());
                returnList.addAll(list);
            }
        }
        return returnList;
    }

    @Override
    public PageInfo<QueryPortMessageInfo> queryPortMessageList(QueryPortMessageRq rq) {
        PageHelper.startPage(rq.getPageNum(),rq.getPageSize());
        List<QueryPortMessageInfo> portMessageInfos = baseMapper.queryPortMessageList(rq);
        PageInfo<QueryPortMessageInfo> pageInfo = new PageInfo<>(portMessageInfos);
        return pageInfo;
    }

    @Override
    public List<QueryPortMessageInfo> exportPortMessage(QueryPortMessageRq rq) {
        List<QueryPortMessageInfo> portMessageInfos = baseMapper.queryPortMessageList(rq);
        return portMessageInfos;
    }

    @Override
    public QueryTimeZoneSwitchingInfo timeZoneSwitching(QueryTimeZoneSwitchingRq rq) throws ParseException {
        QueryTimeZoneSwitchingInfo info = new QueryTimeZoneSwitchingInfo();
        String timeZoneName = baseMapper.queryTimeZoneByPortName(rq.getPortName());
        if (StringUtils.isEmpty(timeZoneName)){
            timeZoneName = baseMapper.queryReceivingStationName(rq.getPortName());
        }
        TimeZone timeZone = TimeZone.getTimeZone(timeZoneName);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateFormat.setTimeZone(timeZone);
        try {
            String dateString = rq.getDate();

            Date date = dateFormat.parse(dateString);

            info.setLocalTime(dateFormat.format(date));
            dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
            info.setUtcTime(dateFormat.format(date));
            TimeZone otherTimeZone = TimeZone.getTimeZone("GMT+8");
            dateFormat.setTimeZone(otherTimeZone);
            info.setNowTime(dateFormat.format(date));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return info;
    }


    @Override
    public List<QueryPortNameInfo> queryPortNameAll(String portName) {
        List<QueryPortNameInfo> portNameList = baseMapper.queryPortNameList(portName);
        List<QueryPortNameInfo> receivingStationList = baseMapper.queryReceivingStationList(portName);
        if (CollectionUtil.isNotEmpty(receivingStationList)){
            portNameList.addAll(receivingStationList);
        }
        if (CollectionUtil.isNotEmpty(portNameList)){
            return portNameList.stream().distinct().collect(Collectors.toList());
        }
        return null;
    }

    @Override
    public boolean updatePortMessageById(UpdatePortMessageRq rq) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id",rq.getId());
        updateWrapper.set("remark",rq.getRemark());
        int update = baseMapper.update(null, updateWrapper);
        if (update == 1){
            return true;
        }
        return false;
    }

    @Override
    public SpBoatsPortMessage queryPortMessageById(Long id) {
        return baseMapper.selectById(id);
    }
}
